<template>
  <div class="coupon_grant">
    <div class="card card-body">
      <div class="container">
        <div class="row">
          <div class="col-sm col-md-3 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px"></div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">油品：</span>
              </div>
              <select
                class="custom-select"
                id="inputGroupSelect01"
                style="width: 80px"
                v-model="queryParams.oilId"
              >
                <option :value="undefined" selected>全部</option>
                <option
                  :value="ele.oilId"
                  v-for="ele in oilList"
                  :key="ele.oilId"
                >
                  {{ ele.oilName + "-" + ele.oilClass + "-" + ele.oilType }}
                </option>
              </select>
            </div>
          </div>
          <div class="col-sm col-md-7"></div>
        </div>
        <div class="row">
          <div class="col-sm col-md-3 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px"></div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">标签：</span>
              </div>
              <select
                class="custom-select"
                id="inputGroupSelect01"
                style="width: 80px"
                v-model="queryParams.labelId"
              >
                <option :value="undefined" selected>全部</option>
                <option :value="ele.id" v-for="ele in labelList" :key="ele.id">
                  {{ ele.labelName }}
                </option>
              </select>
            </div>
          </div>
          <div class="col-sm col-md-8">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px"></div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">日期：</span>
                <el-date-picker
                  v-model="searchParams.data"
                  type="datetimerange"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  size="mini"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="margin-top: 2px; max-width: 350px"
                >
                </el-date-picker>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm col-md-9 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px"></div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >加油频率：</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.startCountNum"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">次</span>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">
                  &lt; 加油次数 &lt;
                </span>
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.endCountNum"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">次</span>
              </div>
            </div>
          </div>
        </div>

        <div class="row">
          <div class="col-sm col-md-9 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px">
                <select
                  class="custom-select custom-select-sm"
                  id="inputGroupSelect01"
                  v-model="queryParams.amtFlag"
                >
                  <option value="1">或者</option>
                  <option value="2">并且</option>
                </select>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >加油金额：</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.startAmt"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">元</span>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">
                  &lt; 加油金额 &lt;
                </span>
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.endAmt"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">元</span>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm col-md-9 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px">
                <select
                  class="custom-select custom-select-sm"
                  id="inputGroupSelect01"
                  v-model="queryParams.litersFlag"
                >
                  <option value="1">或者</option>
                  <option value="2">并且</option>
                </select>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >加油升数：</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.startLiters"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">(L/kg)</span>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">
                  &lt; 加油升数 &lt;
                </span>
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.endLiters"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">(L/kg)</span>
              </div>
            </div>
          </div>
        </div>
        <div class="row mb-0 mt-3" v-show="queryParams.secondFlag == 0">
          <div class="col-sm-4 offset-sm-3">
            <el-button
              type="info"
              size="mini"
              icon="el-icon-arrow-down"
              data-toggle="collapse"
              data-target="#collapseExample"
              aria-expanded="false"
              aria-controls="collapseExample"
              @click="toggleQuery"
              >展开查询</el-button
            >
          </div>
          <div class="col-sm-4">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getList"
              >立刻查询</el-button
            >
          </div>
        </div>
      </div>

      <div class="container collapse" id="collapseExample">
        <hr />
        <div class="row">
          <div class="col-sm col-md-8 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px"></div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">时间：</span>
                <el-date-picker
                  v-model="searchParams.secondDate"
                  type="datetimerange"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  size="mini"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="margin-top: 2px; max-width: 350px"
                >
                </el-date-picker>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm col-md-9 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px"></div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >加油频率：</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model="queryParams.secondStartCountNum"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">次</span>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >&lt; 加油次数 &lt;=</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model="queryParams.secondEndCountNum"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">次</span>
              </div>
            </div>
          </div>
        </div>

        <div class="row">
          <div class="col-sm col-md-9 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px">
                <select
                  class="custom-select custom-select-sm"
                  id="inputGroupSelect01"
                  v-model="queryParams.secondAmtFlag"
                >
                  <option value="1">或者</option>
                  <option value="2">并且</option>
                </select>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >加油金额：</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.secondStartAmt"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">元</span>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">
                  &lt; 加油金额 &lt;=
                </span>
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.secondEndAmt"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">元</span>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm col-md-9 offset-md-1">
            <div class="input-group input-group-sm flex-nowrap">
              <div class="input-group-prepend" style="width: 80px">
                <select
                  class="custom-select custom-select-sm"
                  id="inputGroupSelect01"
                  v-model="queryParams.secondLitersFlag"
                >
                  <option value="1">或者</option>
                  <option value="2">并且</option>
                </select>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >加油升数：</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.secondStartLiters"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">(L/kg)</span>
              </div>
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping"
                  >&lt; 加油升数 &lt;=</span
                >
              </div>
              <input
                type="text"
                class="form-control"
                aria-label="Username"
                aria-describedby="addon-wrapping"
                v-model.number="queryParams.secondEndLiters"
              />
              <div class="input-group-prepend">
                <span class="input-group-text" id="addon-wrapping">(L/kg)</span>
              </div>
            </div>
          </div>
        </div>
        <div class="row mb-0 mt-3" v-show="queryParams.secondFlag == 1">
          <div class="col-sm-4 offset-sm-3">
            <el-button
              type="info"
              size="mini"
              icon="el-icon-arrow-up"
              data-toggle="collapse"
              data-target="#collapseExample"
              aria-expanded="false"
              aria-controls="collapseExample"
              @click="toggleQuery"
              >展开查询</el-button
            >
          </div>
          <div class="col-sm-4">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getList"
              >立刻查询</el-button
            >
          </div>
        </div>
      </div>
    </div>
    <el-table :data="userList" @selection-change="selectionChange" class="mt-5">
      <af-table-column align="left" prop="id" type="selection" />
      <af-table-column label="姓名" align="left" prop="customerName" />
      <af-table-column label="当前归属的标签" align="left" prop="labelName">
        <template slot-scope="scope">
          <span
            class="badge"
            :class="[
              scope.row.labelId === null ? 'badge-danger' : 'badge-success',
            ]"
          >
            {{
              scope.row.labelId === null ? "未标记" : scope.row.labelName
            }}</span
          >
        </template>
      </af-table-column>
      <af-table-column label="手机号" align="left" prop="phone" />
      <af-table-column
        label="最后一笔消费时间"
        align="left"
        prop="createDate"
      />
      <af-table-column
        label="近期详细消费"
        align="left"
        prop="stationName"
        width="160px"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-search"
            class="underline btn24"
            size="mini"
            @click="getConsume(scope.row)"
            >查询</el-button
          >
        </template>
      </af-table-column>
      <af-table-column>
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-search"
            class="underline btn24"
            size="mini"
            @click="toLabeled(scope.row)"
            >发送优惠券</el-button
          >
        </template>
      </af-table-column>
    </el-table>
    <pagination
      v-show="total > 1"
      :total="total"
      :pageSizes="[10, 50, 100, 200]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="发送消息"
      :visible.sync="toLabeledDialog"
      width="600px"
      @close="toLabeledDialogClose"
      append-to-body
    >
      <el-form :model="grantForm" ref="grant" class="form-com-css">
        <el-form-item
          prop="id"
          label="发放模板"
          :rules="{
            required: true,
            message: '请选择发放模板',
            trigger: 'change',
          }"
        >
          <el-select v-model="grantForm.id" clearable placeholder="请选择">
            <el-option
              :label="ele.issueRemark + ' - ' + ele.issueName"
              :value="ele.id"
              :key="ele.id"
              v-for="ele in issueList"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="发放消息"
          prop="msg"
          :rules="{
            required: true,
            message: '请填入方案方式标识',
            trigger: 'blur',
          }"
        >
          <input
            type="text"
            class="form-control"
            v-model="grantForm.msg"
            style="width: 350px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitLable" size="small"
          >确 定</el-button
        >
        <el-button @click="cancelSubmitLabel" size="small">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="近日订单"
      :visible.sync="orderDialog"
      width="750px"
      append-to-body
    >
      <div class="app-container">
        <el-table :data="orderList">
          <!-- <af-table-column label="油站" align="left" prop="stationName" /> -->
          <af-table-column label="订单号" align="left" prop="orderNo" />
          <af-table-column label="油品名称" align="left" prop="oilName" />
          <af-table-column label="油品价格" align="left" prop="oilPirce" />
          <af-table-column label="加油升数" align="left" prop="orderLiters" />
          <af-table-column label="实付金额" align="left" prop="amt" />
          <!-- <af-table-column label="加油枪号" align="left" prop="oilGun" /> -->
          <af-table-column
            label="支付完成时间"
            align="left"
            prop="payDate"
            width="160px"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.payDate, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </af-table-column>
        </el-table>
        <pagination
          v-show="orderList.length > 0"
          :total="orderTotal"
          :page.sync="orderQuery.pageNum"
          :limit.sync="orderQuery.pageSize"
          @pagination="getOrderList"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getLabelUser,
  getLabelList,
  delUserLabel,
  addAndDelCustomerLabel,
  getConsume,
  getOilNameList,
} from "@/api/label/label";
import { getOilListByLogin } from "@/api/dept";
import { listIssue } from "@/api/coupon";

export default {
  name: "Coupon_Grant",
  data() {
    return {
      // 总条数
      total: 0,
      // 客户电子会员卡充值消费记录表格数据
      userList: [],
      searchParams: {
        data: null,
        secondDate: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        oilName: null,
        oilId: null,
        labelId: null,

        beginDate: null,
        endDate: null,

        startCountNum: null,
        endCountNum: null,

        amtFlag: "1",
        startAmt: null,
        endAmt: null,

        litersFlag: "1",
        startLiters: null,
        endLiters: null,

        secondFlag: 0,
        secondBeginDate: null,
        secondEndDate: null,

        secondStartCountNum: null,
        secondEndCountNum: null,

        secondAmtFlag: "1",
        secondStartAmt: null,
        secondEndAmt: null,

        secondLitersFlag: "1",
        secondStartLiters: null,
        secondEndLiters: null,
      },
      orderList: [{}],
      orderOpen: false,
      totalInfo: 0,
      query: {
        pageNum: 1,
        pageSize: 10,
        unionId: null,
        customerName: null,
        recordType: null,
        integral: null,
        stationId: null,
        stationName: null,
        levelId: this.levelId,
      },
      dateRangeCreatedDate: [],
      selectedUser: [],
      checkedUser: [],
      toLabeledDialog: false,
      labelList: [],
      oilList: [],
      currentLabelId: undefined,
      orderDialog: false,
      orderTotal: 0,
      grantForm: {
        id: "",
        msg: "你好",
      },
      orderQuery: {
        pageNum: 1,
        pageSize: 10,
        customerPhone: undefined,
      },
      showQuery: false,
      issueList: [],
    };
  },
  computed: {
    filterSelectedUser() {
      return this.selectedUser.filter((ele) => {
        return !!ele.labelId && ele.labelId != this.currentLabelId;
      });
    },
  },
  created() {
    this.getIssueList();
    this.getList();
    getLabelList()
      .then((res) => {
        if (res.code == 200) {
          this.labelList = res.rows;
        } else {
          throw new Error("");
        }
      })
      .catch(() => {
        this.msgError("拉取标签列表失败，请刷新网页~");
      });
    getOilListByLogin()
      .then((res) => {
        if (res.code == 200) {
          this.oilList = res.rows;
        } else {
          throw new Error("");
        }
      })
      .catch(() => {
        this.msgError("拉取油品列表失败，请刷新网页~");
      });
  },
  methods: {
    getIssueList() {
      // 拉取领取方式列表
      listIssue({
        pageNum: 1,
        pageSize: 1000,
        issueName: null,
        issueRemark: null,
        issueType: "2", // 网页类型
        status: "1", // 启动的
      })
        .then((res) => {
          // console.log('你好',res)
          if (res.code == 200) {
            if (res.rows == null) {
              this.issueList = [];
            } else {
              this.issueList = res.rows;
            }
          } else {
            throw new Error("");
          }
        })
        .catch((err) => {
          this.msgError("亲，拉取领取方式列表失败~");
        });
    },
    getConsume(row) {
      this.orderQuery.customerPhone = row.phone;
      this.getOrderList();
    },
    getOrderList() {
      getConsume(this.orderQuery)
        .then((res) => {
          if (res.code == 200) {
            this.orderList = res.rows;
            this.orderTotal = res.total;
            this.orderDialog = true;
          } else {
            throw new Error("");
          }
        })
        .catch(() => {
          this.msgError("查询近日订单~");
        });
    },
    toLabeledDialogClose() {
      this.selectedUser = [];
      this.checkedUser = [];
    },
    handleSubmitLable() {
      const addArr = [];
      this.selectedUser.map((ele) => {
        if (ele.labelId != this.currentLabelId && !ele.labelId)
          addArr.push(ele.phone);
      });
      addAndDelCustomerLabel({
        labelId: this.currentLabelId,
        flag: "0",
        phoneList: addArr,
      })
        .then((res) => {
          if (res.code == 200) {
            this.getList();
            this.msgSuccess("批量拉入标签成功~");
            this.toLabeledDialog = false;
          } else {
            throw new Error("");
          }
        })
        .catch(() => {
          this.msgError("批量拉入标签失败，请刷新网页后重试~");
          this.toLabeledDialog = false;
        });
    },
    cancelSubmitLabel() {
      this.getList();
      this.toLabeledDialog = false;
    },
    translationLabel(id) {
      const label = this.labelList.find((ele) => {
        return id == ele.id;
      });
      if (label == undefined) {
        return "";
      }
      return label.labelName;
    },
    toUnLableed(row) {
      const delArr = [];
      if (this.selectedUser.length == 0) {
        delArr.push(row.phone);
      } else {
        this.selectedUser.map((ele) => {
          if (!!ele.phone) {
            delArr.push(ele.phone);
          }
        });
      }

      delUserLabel({ phoneList: delArr })
        .then((res) => {
          if (res.code == 200) {
            this.getList();
            this.msgSuccess("删除用户标签成功~");
          } else {
            throw new Error("");
          }
        })
        .catch(() => {
          this.msgError("删除用户标签状态失败，请刷新后重试~");
        });
    },
    toClearLabel(row) {
      const delArr = [];
      if (this.checkedUser.length == 0) {
        this.checkedUser = [row];
        delArr.push(row.phone);
      } else {
        this.checkedUser.map((ele) => {
          delArr.push(ele.phone);
        });
      }

      delUserLabel({ phoneList: delArr })
        .then((res) => {
          if (res.code == 200) {
            this.checkedUser.map((ele) => {
              if (delArr.includes(ele.phone)) {
                ele.labelId = null;
                ele.labelName = null;
              }
            });
            this.msgSuccess("删除用户标签成功~");
          } else {
            throw new Error("");
          }
        })
        .catch(() => {
          this.msgError("删除用户标签状态失败，请刷新后重试~");
        });
    },
    selectionChange(selection) {
      this.checkedUser = [];
      this.selectedUser = selection;
    },
    checkChange(selection) {
      this.checkedUser = selection;
    },
    usageTypeFotmat(row, column) {
      if (row.usageType === "+") {
        return "充值";
      } else if (row.usageType === "-") {
        return "消费";
      }
    },
    cardOilsTypeFotmat(row, column) {
      if (row.cardOilsType === "1") {
        return "汽油";
      } else if (row.cardOilsType === "2") {
        return "柴油";
      } else if (row.cardOilsType === "3") {
        return "非油品";
      } else if (row.cardOilsType === "4") {
        return "LNG";
      } else if (row.cardOilsType === "5") {
        return "CNG";
      }
    },
    /** 查询客户电子会员卡充值消费记录列表 */
    getList() {
      if (this.searchParams.data === null) {
        this.queryParams.beginDate = null;
        this.queryParams.endDate = null;
      } else {
        this.queryParams.beginDate = this.searchParams.data[0];
        this.queryParams.endDate = this.searchParams.data[1];
      }
      if (this.searchParams.secondDate === null) {
        this.queryParams.secondBeginDate = null;
        this.queryParams.secondEndDate = null;
      } else {
        this.queryParams.secondBeginDate = this.searchParams.secondDate[0];
        this.queryParams.secondEndDate = this.searchParams.secondDate[1];
      }
      getLabelUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    orderOpenMethod() {
      this.orderOpen = true;
    },
    toLabeled(idAndPhone) {
      this.toLabeledDialog = true;
    },
    toggleQuery() {
      if (this.queryParams.secondFlag == 0) {
        this.queryParams.secondFlag = 1;
      } else {
        this.queryParams.secondFlag = 0;
      }
    },
  },
};
</script>
<style scoped lang="scss">
.coupon_grant {
  .container {
    .row {
      margin-bottom: 10px;
    }
  }
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
